/**
 * Created by Administrator on 2016/8/19.
 */

swan_pc
    .factory("getAllOrganization",function($resource){
        return $resource('/api/organizations',{},{
            'query': {
                method: 'GET',
                isArray:true
            }
        })
    })
    .controller("JoinOrgController",function($scope,$q,getAllOrganization,messageManage){
        //获取所有组织(我未加入的)
        $scope.getAllOrg=function() {
            var def=$q.defer();
            var pro=def.promise;
            $scope.OrgList=[];
            getAllOrganization.query(function (result) {
                $scope.OrgList=result;
                $scope.data=$scope.OrgList;
                //console.log(result);
                console.log($scope.data);
                def.resolve();
            },function(result){
            })
            pro.then(function(){
                $scope.pageSize = 5;
                $scope.initPageSort();
            })
        };
        $scope.getAllOrg();

        //个人向组织发出加入申请
        $scope.addFriendMessage={
            "parameter":null,
            "type": "JOIN_APPLY_FROM_USER"
        };
        $scope.ApplyToJoinOrg=function(id){
            id=id.toString();
            $scope.addFriendMessage.parameter="{'orgId':'"+id+"'}";
            messageManage.save($scope.addFriendMessage,function(result){
                    swal("申请已发出，请耐心等待组织回复","","success");
                    console.log(result);
                },function(result){
                    swal("申请失败","","error");
                    console.log(result);
                }
            )
        };

        //分页控制实现
        $scope.initPageSort=function(){
            //分页总数
            //console.log($scope.data.length+" "+$scope.pageSize);
            $scope.pages = Math.ceil($scope.data.length / $scope.pageSize); //分页数
            $scope.newPages = $scope.pages > 5 ? 5 : $scope.pages;
            $scope.pageList = [];
            $scope.selPage = 1;
            //设置表格数据源(分页)
            $scope.setData = function () {
                $scope.items = $scope.data.slice(($scope.pageSize * ($scope.selPage - 1)), ($scope.selPage * $scope.pageSize));//通过当前页数筛选出表格当前显示数据
            }
            $scope.items = $scope.data.slice(0, $scope.pageSize);
            //分页要repeat的数组
            for (var i = 0; i < $scope.newPages; i++) {
                $scope.pageList.push(i + 1);
            }
            //打印当前选中页索引
            $scope.selectPage = function (page) {
                //不能小于1大于最大
                if (page < 1 || page > $scope.pages) return;
                //最多显示分页数5
                if (page > 2) {
                    //因为只显示5个页数，大于2页开始分页转换
                    var newpageList = [];
                    for (var i = (page - 3); i < ((page + 2) > $scope.pages ? $scope.pages : (page + 2)); i++) {
                        newpageList.push(i + 1);
                    }
                    $scope.pageList = newpageList;
                }
                $scope.selPage = page;
                $scope.setData();
                $scope.isActivePage(page);
                //console.log("选择的页：" + page);
            }
        }
        //设置当前选中页样式
        $scope.isActivePage = function (page) {
            return $scope.selPage == page;
        };
        //上一页
        $scope.Previous = function () {
            $scope.selectPage($scope.selPage - 1);
        }
        //下一页
        $scope.Next = function () {
            $scope.selectPage($scope.selPage + 1);
        };
        $scope.$watch(function(){
            return $scope.pageSize;
        },function(){
            //console.log($scope.data);
            if($scope.data!=undefined) {
                $scope.initPageSort();
            }
        })
        //排序实现
        $scope.col="Name";
        $scope.desc=1;
        /*$scope.orderList=function(x){
         console.log(codefans_net_CC2PY(x.Name));
         return x?codefans_net_CC2PY(x.Name):"";
         }*/
    });
